package com.sihai.ehr.web.action.sign;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.FinancialReportForm;
import com.sihai.ehr.model.biz.FinancialReportFormAuditing;
import com.sihai.ehr.model.biz.FinancialReportFormDetail;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.FinancialReportFormService;
import com.sihai.ehr.service.UserService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.ArrayList;
import java.util.List;

/**
 * author: wxn
 * since: 13-4-14 下午12:32
 * 财务签核系统 - 财务报支单Action
 */
@Results({
        @Result(name = "list", location = "/WEB-INF/pages/sign/financial_report_form_list.jsp"),
        @Result(name = "add", location = "/WEB-INF/pages/sign/financial_report_form_input.jsp"),
        @Result(name = "audit", location = "financial_report_form_audit.jsp")
})
public class FinancialReportFormAction extends SignBaseAction {
    @BeanInject
    private FinancialReportFormService financialReportFormService;
    @BeanInject
    private UserService userService;

    private LimitedList<FinancialReportForm> financialReportForms;
    private FinancialReportForm financialReportForm;
    private List<FinancialReportFormDetail> formDetails;

    private List<List<FinancialReportFormAuditing>> audits;

    //查找用户自己的数据
    @Action("/public/financial-report-form")
    public String listByUser() throws Exception {
        userType = 2;
        String temp = execute();
        showChildOrg = false;
        return temp;
    }

    @Action("/sign/financial-report-form")
    @Override
    public String execute() throws Exception {
        showChildOrg = true;
        init();
        User user = null;
        if(userType == 2){
            user = userService.getUser(getCurrentUserId());
        }
        financialReportForms = financialReportFormService.findFinancialReportForm(user, searchFormPojo, createLimit(), createSorter());
        this.paging(financialReportForms);
        if(userType == 1){
            this.setPageUrl("sign/financial-report-form.xhtml");
        }else if(userType == 2){
            this.setPageUrl("public/financial-report-form.xhtml");
        }
        return "list";
    }

    @Action("/sign/financial-report-form-input")
    public String toAdd() throws Exception {
        return "add";
    }

    @Action("/public/financial-report-form-input")
    public String toAddByUser() throws Exception {
        userType = 2;
        return "add";
    }

    @Action("/public/financial-report-form-save")
    public String saveByUser() throws Exception {
        userType = 2;
        return save();
    }

    //保存
    @Action("/sign/financial-report-form-save")
    public String save() throws Exception {
        User user = userService.getUser(getCurrentUserId());
        String[] detailInfos = getParameterValues("detail_info_input");
        String[] detailMoneys = getParameterValues("detail_money_input");
        String[] detailOperators = getParameterValues("detail_operator_input");
        String[] detailAuditMoneys = getParameterValues("detail_audit_money_input");
        if(financialReportForm == null || detailInfos == null || detailMoneys == null || detailOperators == null || detailAuditMoneys == null){
            return execute();
        }
        int length = detailInfos.length;
        List<FinancialReportFormDetail> details = new ArrayList<FinancialReportFormDetail>(length);
        for(int i=0; i<length; i++){
            FinancialReportFormDetail detail = new FinancialReportFormDetail();
            detail.setInfo(detailInfos[i]);
            detail.setMoney(NumberUtils.createFloat(detailMoneys[i]));
            detail.setOperator(detailOperators[i]);
            detail.setAuditMoney(NumberUtils.createFloat(detailAuditMoneys[i]));
            details.add(detail);
        }
        financialReportForm.setWork(user == null ? null : user.getWork());
        financialReportForm.setStatus(AuditStatus.wait_audit);
        financialReportFormService.save(financialReportForm, details);
        return execute();
    }

    @Action("/sign/financial-report-form-audit")
    public String toAudit() throws Exception {
        //得到审核对象
        if(financialReportForm == null || financialReportForm.getId() == null){
            return execute();
        }
        financialReportForm = financialReportFormService.get(financialReportForm.getId());
        formDetails = financialReportFormService.findFinancialReportFormDetails(financialReportForm);

        //得到当前审核历史记录(没有就先保存一个审核流程次数)
        audits = financialReportFormService.findFinancialReportFormAuditing(financialReportForm.getId());
        if(audits == null || audits.isEmpty()){
            financialReportFormService.saveFinancialReportFormAuditingProcess(financialReportForm.getId());
            audits = financialReportFormService.findFinancialReportFormAuditing(financialReportForm.getId());
        }
        //检查当前登录用户是否可以审核当前结点
        if(financialReportForm.getWork() != null && audits != null && !audits.isEmpty()){
            checkCanAudit(financialReportForm.getWork().getDuty(), audits.get(audits.size()-1));
        }
        currentAuditType = AuditType.audit_financial_report_form.toString();

        return "audit";
    }

    public LimitedList<FinancialReportForm> getFinancialReportForms() {
        return financialReportForms;
    }

    public FinancialReportForm getFinancialReportForm() {
        return financialReportForm;
    }

    public void setFinancialReportForm(FinancialReportForm financialReportForm) {
        this.financialReportForm = financialReportForm;
    }

    public List<List<FinancialReportFormAuditing>> getAudits() {
        return audits;
    }

    public List<FinancialReportFormDetail> getFormDetails() {
        return formDetails;
    }


}
